
#ifndef CameraItem_H
#define CameraItem_H

#include <QDeclarativeItem>
#include <QPainter>
#include <QTimer>
#include <QTime>

#include <QAbstractVideoSurface>
#include <QVideoSurfaceFormat>


// Unlike the other APIs in QtMobility,
// the QtMobility Multimedia API is not in the QtMobility namespace
#include <QCamera>
#include <QCameraImageCapture>
#include <QCameraViewfinder>
#include <QVideoRendererControl>


/* ***************************************************************************
* MyVideoSurface
*/
class CameraUpdateIF
{
public:
    virtual void updateVideo() = 0;
    virtual void differenceImage() = 0;
    virtual void sameImage() = 0;
    virtual void kwh(QString) = 0;

};
class MyVideoSurface: public QAbstractVideoSurface
{
    Q_OBJECT

public:
    MyVideoSurface(QDeclarativeItem* widget, CameraUpdateIF* target, QObject * parent = 0);
    ~MyVideoSurface();

    bool start(const QVideoSurfaceFormat &format);

    bool present(const QVideoFrame &frame);

    QList<QVideoFrame::PixelFormat> supportedPixelFormats(
            QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const;

    bool isFormatSupported(const QVideoSurfaceFormat &format, QVideoSurfaceFormat *similar) const;

    void paint(QPainter*);

    bool framesExists();

    QSize targetImageSize();
    QImage::Format targetImageFormat();

    void hideViewFinder(bool);

private:
    bool isSameImage(QImage* newImage);
    float calculate(int ms);


public:
    QImage                  m_targetImage;

private:
    QDeclarativeItem*       m_targetWidget;
    CameraUpdateIF*         m_target;
    QVideoFrame             m_frame;
    QImage::Format          m_imageFormat;
    QVideoSurfaceFormat     m_videoFormat;
    QImage                  m_sourceImage;

    int                     m_handleFrameCount;
    bool                    m_differentFrame;
    bool                    m_hideViewFinder;

    QTime                   m_lastTime;
    bool                    m_ledLights;
};


/* ***************************************************************************
* CameraItem
*/
class CameraItem : public QDeclarativeItem, public CameraUpdateIF
{
    Q_OBJECT

public:
    explicit CameraItem(QDeclarativeItem *parent = 0);
    virtual ~CameraItem();

    void updateVideo();
    void differenceImage();
    void sameImage();
    void kwh(QString);

    void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);

public:
    Q_INVOKABLE void enableCamera(QVariant enable);
    Q_INVOKABLE void enableCameraAt(QVariant enable,QVariant cameraIndex);
    Q_INVOKABLE void pauseCamera();

    Q_INVOKABLE void hideViewFinder();
    Q_INVOKABLE void showViewFinder();

private slots:
    void startCamera();
    void stopCamera();
    void error(QCamera::Error);

signals:
    void ledOff();
    void ledOn();
    void kwhWaste(QString value);

private:
    void keepPaintingStoredPicture();


private:
    QCamera*                m_camera;
    MyVideoSurface*         m_myVideoSurface;
    bool                    m_showViewFinder;

    bool                    m_keepPaintingStoredPicture;
    QImage                  m_lastPicture;

    QList<QByteArray>       m_devices;
    int                     m_deviceId;

};

QML_DECLARE_TYPE(CameraItem)

#endif // CameraItem_H
